package com.amigo.online.provider.compare.quartzjob;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.amigo.online.provider.common.compare.AbstractBasicCompareService;
import com.amigo.online.provider.common.entity.MusicMysqlInfo;
import com.amigo.online.provider.common.entity.MusicSolrInfo;
import com.amigo.online.provider.compare.service.MusicComparisonService;
import com.amigo.online.provider.sync.handle.data.solr.service.MusicSolrService;
import com.amigo.online.provider.sync.handle.service.DataHandler;

public abstract class AbstractMusicComparisonJob extends AbstractCompareJob {

    @Autowired
    ExecutorService executorService;

    @Autowired
    MusicComparisonService musicComparisonService;

    @Autowired
    MusicSolrService musicSolrService;

    @Autowired
    @Qualifier("MusicSolrDataHandler")
    DataHandler musicSolrDataHandler;
	
	/*@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		List<MusicSolrInfo> musicSolrList = new ArrayList<>();
		List<MusicMysqlInfo> musicMysqlList = new ArrayList<>();
		context.getJobDetail();
		// mysql 和mongo 数据比对
		System.out.println("Mysql-------to----------mongo");

		int maxId = musicComparisonService.getMaxId();
		// 异步操作，把查询的数据塞入到全局变量中
		int index = maxId / 1000 + 1;
		List<FutureTask<List<MusicMysqlInfo>>> mysqlfutureList = new ArrayList<>(maxId);
		List<FutureTask<List<MusicSolrInfo>>> solrfutureList = new ArrayList<>(maxId);
		for (int i = 1; i < index; i++) {
			int startId = i + 1000 * (i - 1);
			int endId = startId + 999;
			
			MysqlMusicTask mysqlMusicTask = new MysqlMusicTask(startId, endId);
			FutureTask<List<MusicMysqlInfo>> mysqlfuture = new FutureTask<>(mysqlMusicTask);
			executorService.submit(mysqlfuture);
			mysqlfutureList.add(mysqlfuture);

			SolrMusicTask solrMusicTask = new SolrMusicTask(startId, endId);
			FutureTask<List<MusicSolrInfo>> solrfuture = new FutureTask<>(solrMusicTask);
			executorService.submit(solrfuture);
			solrfutureList.add(solrfuture);

		}
		executorService.shutdown();
		try {
			for (FutureTask<List<MusicMysqlInfo>> temp : mysqlfutureList) {
				List<MusicMysqlInfo> partMusicMysqlList = temp.get();
				musicMysqlList.addAll(partMusicMysqlList);
			}
			for (FutureTask<List<MusicSolrInfo>> temp : solrfutureList) {
				List<MusicSolrInfo> partMusicMongoList = temp.get();
				musicSolrList.addAll(partMusicMongoList);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		compare(musicSolrList, musicMysqlList);
	}*/

    private void compare(List<MusicSolrInfo> musicSolrList, List<MusicMysqlInfo> musicMysqlList) {
        Map<Long, MusicSolrInfo> solrMap = musicSolrList.stream()
                .collect(Collectors.toMap(MusicSolrInfo::getId, musicSolrInfo -> musicSolrInfo));
        for (MusicMysqlInfo temp : musicMysqlList) {
            MusicSolrInfo musicSolrInfo = solrMap.get(temp.getId());
            if (musicSolrInfo == null) {
                executorService.execute(() -> {
                    musicSolrDataHandler.insertObject(temp);
                });
            } else {
                Map<String, Object> solrWrongMap = new HashMap<>(16);
                AbstractBasicCompareService.compare(temp, musicSolrInfo, solrWrongMap);
                if (!solrWrongMap.isEmpty()) {
                    executorService.execute(() -> {
                        musicSolrDataHandler.updateInfoByIdThroughHashMap(temp.getId(), solrWrongMap);
                    });
                }
            }


        }
    }


}
